//
//  mm_addAndModifyViewController.m
//  HondaPro_2
//
//  Created by Eric on 13-6-28.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "mm_addAndModifyViewController.h"

@interface mm_addAndModifyViewController ()

@property (assign,nonatomic) IBOutlet UIScrollView *scrollView;

/** 顶部标签页按钮 */
@property(nonatomic,strong)UIButton *repairTopPageBtn;
@property(nonatomic,strong)UIButton *partTopPageBtn;
/** 底部标签页按钮 */
@property(nonatomic,strong)UIButton *repairBottomPageBtn;
@property(nonatomic,strong)UIButton *partBottomPageBtn;

/** 存放维修项目列表标标签页 */
@property(nonatomic,strong)NSArray *repairViews;
/** 存放零件项目表标签页 */
@property(nonatomic,strong)NSArray *partViews;

@property(nonatomic,strong)AddRepairItemSubView *addRepairItemSubViewIns;
@property (nonatomic,strong) AddPartListSubView *addPartListSubViewIns;

@property(nonatomic,strong)DisRepairItemSubView *disRepairItemSubViewIns;
@property(nonatomic,strong)DisPartListSubView *disPartListSubViewIns;

@end

@implementation mm_addAndModifyViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createScrollView];
    
    [self createAddBtn];
    [self createdisBtn];
    
    if (self.isRepairPage) {
        self.repairTopPageBtn.selected = YES;
        self.repairBottomPageBtn.selected = YES;
        [self.repairViews makeObjectsPerformSelector:@selector(setHidden:) withObject:NO];
    }else{
        self.partTopPageBtn.selected = YES;
        self.partBottomPageBtn.selected = YES;
        [self.partViews makeObjectsPerformSelector:@selector(setHidden:) withObject:NO];
    }
    //注册消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDataSubView1) name:SECOND_REPAIR_ITEM_LIST_MSG_ object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDataSubView2) name:SECOND_PART_LIST_MSG_ object:nil];
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDataSubView3) name:SECOND_OTHER_CHARGE_LIST_MSG_ object:nil];
    
    //弹出代码选择 框 的消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popUpTheCodeSelectionView) name:MM_POP_UP_CODE_SELECTION_MSG_ object:nil];
    
    //注册消息，用于滚动scrollView
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollViewScrollByOffSet) name:@"mm_addAndModifyViewControllerScrollViewScroll" object:nil];
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)updateDataSubView1
{
    [self.disRepairItemSubViewIns setNeedsDisplay];
    //调用代理方法
    if ([self.delegate respondsToSelector:@selector(repairProjectListDataChanged)]) {
        [self.delegate repairProjectListDataChanged];
    }
}

-(void)updateDataSubView2
{
    [self.disPartListSubViewIns setNeedsDisplay];
    //调用代理方法
    if ([self.delegate respondsToSelector:@selector(parttListDataChanged)]) {
        [self.delegate parttListDataChanged];
    }
}


#pragma mark - 创建控件

-(void)createScrollView
{
    [self.scrollView setContentSize:CGSizeMake(self.scrollView.contentSize.width, 900)];
    self.scrollView.backgroundColor = [UIColor whiteColor];
}

//scrollView滚动一定位置
-(void)scrollViewScrollByOffSet
{
    [self.scrollView setContentOffset:CGPointMake(0, 300)];
}

-(void)createAddBtn
{
    self.repairTopPageBtn = [self setupButtonWithTitle:@"维修项目录入" frame:CGRectMake(0, 0, 120, 50) method:@selector(addRepairBtnPressed:)];

    
    self.partTopPageBtn = [self setupButtonWithTitle:@"零件录入" frame:CGRectMake(150, 0, 120, 50) method:@selector(addPartListBtnPressed:)];
    
    
    UIView *bottomLbl = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.scrollView.frame.size.width, 3)];
    bottomLbl.backgroundColor = [UIColor redColor];
    [self.scrollView addSubview:bottomLbl];
}

-(UIButton *)setupButtonWithTitle:(NSString *)title frame:(CGRect)frame method:(SEL)method{
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = frame;
    [btn1 setTitle:title forState:UIControlStateNormal];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"mm_red_btn.png"] forState:UIControlStateSelected];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"mm_gray_btn.png"] forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    btn1.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn1 addTarget:self action:method forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:btn1];
    return btn1;
}

-(void)createdisBtn
{
    self.repairBottomPageBtn = [self setupButtonWithTitle:@"维修项目" frame:CGRectMake(0, 570, 120, 40) method:@selector(addRepairBtnPressed:)];
    
    self.partBottomPageBtn = [self setupButtonWithTitle:@"零件列表" frame:CGRectMake(150, 570, 120, 40) method:@selector(addPartListBtnPressed:)];
    
    UIView *bottomLbl = [[UIView alloc] initWithFrame:CGRectMake(0, 610, self.scrollView.frame.size.width, 3)];
    bottomLbl.backgroundColor = [UIColor redColor];
    [self.scrollView addSubview:bottomLbl];

}

#pragma mark - 回调方法

-(void)addRepairBtnPressed:(UIButton *)sender
{
    [self showViewWithFlag:YES];
}

-(void)addPartListBtnPressed:(UIButton *)sender
{
    [self showViewWithFlag:NO];
}

-(void)showViewWithFlag:(BOOL)flag{
    self.repairTopPageBtn.selected = flag;
    self.repairBottomPageBtn.selected = flag;
    self.partTopPageBtn.selected = !flag;
    self.partBottomPageBtn.selected = !flag;
    self.addRepairItemSubViewIns.hidden = !flag;
    self.disRepairItemSubViewIns.hidden = !flag;
    self.addPartListSubViewIns.hidden = flag;
    self.disPartListSubViewIns.hidden = flag;
}


- (IBAction)backBtnPressed:(UIButton *)sender
{
    //跳回上一级页面
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)homeBtnPressed:(UIButton *)sender
{
    NSLog(@"homeBtnPressed !!!");
    
    //跳回主界面
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - 列表懒加载

-(AddRepairItemSubView *)addRepairItemSubViewIns{
    if (_addRepairItemSubViewIns == nil) {
        _addRepairItemSubViewIns = [[AddRepairItemSubView alloc] initWithFrame:CGRectMake(0, 60, self.scrollView.frame.size.width, 500)];
        _addRepairItemSubViewIns.backgroundColor = [UIColor whiteColor];
        _addRepairItemSubViewIns.discount = self.repairDiscount;
        _addRepairItemSubViewIns.labourPrice = self.labourPrice;
        _addRepairItemSubViewIns.jobSheetLabourPrice = self.jobSheetLabourPrice;
        _addRepairItemSubViewIns.carType=self.carType;
        _addRepairItemSubViewIns.carTypeCode=self.carTypeCode;
        _addRepairItemSubViewIns.hidden = YES;
        [self.scrollView addSubview:_addRepairItemSubViewIns];
    }
    return _addRepairItemSubViewIns;
}

-(AddPartListSubView *)addPartListSubViewIns{
    if (_addPartListSubViewIns == nil) {
        _addPartListSubViewIns = [[AddPartListSubView alloc] initWithFrame:CGRectMake(0, 60, self.scrollView.frame.size.width, 500)];
        _addPartListSubViewIns.backgroundColor = [UIColor whiteColor];
        _addPartListSubViewIns.discount = self.partDiscount;
        _addPartListSubViewIns.hidden = YES;
        [self.scrollView addSubview:_addPartListSubViewIns];
    }
    return _addPartListSubViewIns;
}


-(DisRepairItemSubView *)disRepairItemSubViewIns{
    if (_disRepairItemSubViewIns == nil) {
        _disRepairItemSubViewIns = [[DisRepairItemSubView alloc] initWithFrame:CGRectMake(0, 620, self.scrollView.frame.size.width, 250)];
        _disRepairItemSubViewIns.backgroundColor = [UIColor whiteColor];
        _disRepairItemSubViewIns.hidden = YES;
        [self.scrollView addSubview:_disRepairItemSubViewIns];
    }
    return _disRepairItemSubViewIns;
}

-(DisPartListSubView *)disPartListSubViewIns{
    if (_disPartListSubViewIns == nil) {
        _disPartListSubViewIns = [[DisPartListSubView alloc] initWithFrame:CGRectMake(0, 620, self.scrollView.frame.size.width, 250)];
        _disPartListSubViewIns.backgroundColor = [UIColor whiteColor];
        _disPartListSubViewIns.hidden = YES;
        [self.scrollView addSubview:_disPartListSubViewIns];
    }
    return _disPartListSubViewIns;
}


-(NSArray *)repairViews{
    if (_repairViews == nil) {
        _repairViews = @[self.addRepairItemSubViewIns,self.disRepairItemSubViewIns];
    }
    return _repairViews;
}

-(NSArray *)partViews{
    if (_partViews == nil) {
        _partViews = @[self.addPartListSubViewIns,self.disPartListSubViewIns];
    }
    return _partViews;
}

//弹出代码选择的框
-(void)popUpTheCodeSelectionView
{
    //弹出代码选择的弹出框
    
    _popUpViewDis = nil;
    _popUpViewDis = [[mm_codeItemForRepairItemViewController alloc] initWithNibName:@"mm_codeItemForRepairItemViewController" bundle:nil];
    [self presentPopupViewController:_popUpViewDis animationType:MJPopupViewAnimationFade];
}

//代码选择 弹出框 退出的时候调用的方法
-(void)dismissPopupViewControllerWithanimationType:(MJPopupViewAnimation)animationType
{
    [super dismissPopupViewControllerWithanimationType:animationType];

}

@end
